#include "dialog.h"
#include "ui_logindia.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/new/prefix1/QQ.png"));
    this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());


    connect(ui->pbclose,SIGNAL(clicked()),
            this,SLOT(close()));

    connect(ui->pushButton,SIGNAL(clicked()),
            this,SLOT(btnClickedSlot()));

    connect(ui->login,SIGNAL(clicked()),
            this,SLOT(loginSlot()));
    movie = new QMovie(":/new/prefix1/Sakura.gif");
    ui->bgp->setMovie(movie);
    movie->start();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::loginSlot()
{
    static int index = 0;
    QString admin = ui->admin->text();
    QString password = ui->password->text();

    if(admin == "")
    {
        qDebug()<<"请输入用户名";
        return;
    }
    else if(admin != ""&&password == "")
    {
        qDebug()<<"请输入密码";
        return;
    }

    for(int i = 0;i<=index;i++)
    {
        if(admin == adm[i])
        {
            qDebug()<<"用户名已存在";
            return;
        }
    }
    adm[index] = admin;
    pas[index] = password;

    index++;
    qDebug()<<"注册成功";
    QMessageBox::information(this,"KunChat", "注册成功！");
}

void Dialog::btnClickedSlot()
{
    QString admin = ui->admin->text();
    QString password = ui->password->text();
    if(admin == ""||password=="")
    {
        qDebug() << "请输入账号和密码";
        return;
    }
    for(int i = 0;i<=index;i++)
    {
        if(admin == adm[i] && password == pas[i])
        {
            qDebug() << "登陆成功";
            QMessageBox::information(this,"KunChat", "登录成功！");
//            kun = new KunWar();
//            kun->show();
            connect(this,SIGNAL(LoginSuccess(QString)),
                        this,SLOT(IDSendSlot(QString)));
            emit(LoginSuccess(admin));
            close();
            return;
        }
    }

    QMessageBox::warning(this,"warning", "账号或密码错误！");

    return;
}

void Dialog::IDSendSlot(QString id)
{
    kun->kunSetId("ikun:"+id);
}
